<template>
  <el-dialog
    v-model="dialog.visible"
    title="代码生成"
    @open="openHandler"
    @close="closeHandler"
    :close-on-click-modal="false"
    append-to-body
    center
  >
    <el-form
      ref="dataForm"
      :rules="formRules"
      :model="entity"
      label-suffix="："
    >
      <el-row class="padding-lr-20" :span="24" :gutter="20">
        <el-col :span="12">
          <el-form-item prop="tableName" label="表名">
            <el-input v-model="entity.tableName" readonly />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="tablePrefix" label="表名前缀">
            <el-input
              v-model="entity.tablePrefix"
              placeholder="请输入表名前缀"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row class="padding-lr-20" :span="24" :gutter="20">
        <el-col :span="12">
          <el-form-item prop="parentPackage" label="包名">
            <el-input
              v-model="entity.parentPackage"
              placeholder="请输入包名"
              clearable
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item prop="moduleName" label="模块名">
            <el-input
              v-model="entity.moduleName"
              placeholder="请输入模块名"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row class="padding-lr-20" :span="24" :gutter="20">
        <el-col :span="24">
          <el-form-item prop="author" label="作者">
            <el-input
              v-model="entity.author"
              type="textarea"
              :rows="3"
              placeholder="请输入作者"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <el-button size="small" @click="dialog.visible = false">取 消</el-button>
      <el-button type="primary" size="small" @click="generateHandler">
        生 成
      </el-button>
    </template>
  </el-dialog>
</template>
<script>
import { generate } from '@/api/codegen'

export default {
  name: 'Generate',
  props: {
    dialog: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      entity: {},
      formRules: {
        tableName: [{ required: true, message: '请输入表名', trigger: 'blur' }],
        parentPackage: [
          { required: true, message: '请输入包名', trigger: 'blur' },
        ],
        moduleName: [
          { required: true, message: '请输入模块名', trigger: 'blur' },
        ],
        author: [{ required: true, message: '请输入作者', trigger: 'blur' }],
      },
    }
  },
  methods: {
    openHandler() {
      this.entity.dataSourceName = this.dialog.dataSourceName
      this.entity.tableName = this.dialog.tableName
    },
    closeHandler() {
      this.entity.dataSourceName = null
      this.entity.tableName = null
    },
    generateHandler() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          generate(this.entity).then((res) => {
            this.dialog.visible = false
            this.$message({
              showClose: true,
              message: '操作成功',
              type: 'success',
            })

            let blob = new Blob([res.data])
            let a = document.createElement('a')
            document.body.appendChild(a)
            a.download = this.entity.tableName + '.zip'
            a.style.display = 'none'
            a.href = URL.createObjectURL(blob)
            a.click()
            document.body.removeChild(a)
          })
        }
      })
    },
  },
}
</script>
